package com.exartisan.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.exartisan.bean.GalleryContent;
import com.exartisan.thanhdoanhn.PhotoActivity;
import com.exartisan.thanhdoanhn.R;
import com.exartisan.thanhdoanhn.VideoActivity;
import com.exartisan.util.DateTimeFormat;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GalleryAdapter extends BaseAdapter{

	Context context;
	boolean isPhotoGallery;
	ArrayList<GalleryContent> galleryContents;

	public GalleryAdapter(Context context,ArrayList<GalleryContent> galleryContents, boolean isPhotoGallery) {
		this.context = context;
		this.isPhotoGallery = isPhotoGallery;
		this.galleryContents = galleryContents;
	}

	@Override
	public int getCount() {
		return galleryContents.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View listView;
		ViewHolder holder;
 
		if (convertView == null) {
 
			listView = new View(context);
			
			holder = new ViewHolder();
			
			//set row layout
			if (isPhotoGallery) 
			listView = inflater.inflate(R.layout.row_photo_gallery, null);  
			else listView = inflater.inflate(R.layout.row_video_gallery, null);
			
			holder.imgDesc = (ImageView) listView.findViewById(R.id.imgDescription);
			holder.imgAvatar = (ImageView) listView.findViewById(R.id.imgCreator);
			holder.tvTitle = (TextView) listView.findViewById(R.id.tvTitle);
			holder.tvUserName = (TextView) listView.findViewById(R.id.tvCreator);
			holder.tvCreatedTime = (TextView) listView.findViewById(R.id.tvCreatedDate);
			holder.tvLikes = (TextView) listView.findViewById(R.id.tvLikes);
			holder.tvComments = (TextView) listView.findViewById(R.id.tvComments);
			holder.tvViewCount = (TextView) listView.findViewById(R.id.tvViews);
			listView.setTag(holder);
			
 
		} else {
			listView = (View) convertView;
			holder = (ViewHolder) listView.getTag();
		}
		
		GalleryContent a = galleryContents.get(position);
		
		//older.imgAvatar.setImageUrl(a.getAvatar());
		//holder.imgDesc.setImageUrl(a.getResourceUrl());
		
		ImageLoader.getInstance().displayImage(a.getAvatar(), holder.imgAvatar);
		ImageLoader.getInstance().displayImage(a.getResourceUrl(), holder.imgDesc);
		holder.tvTitle.setText(a.getDescription());
		holder.tvCreatedTime.setText(DateTimeFormat.format(a.getDateCreate(), DateTimeFormat.SHORT_VALUE));
		holder.tvUserName.setText(a.getFullName());
		
		final int pos = position;
		
		listView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (isPhotoGallery) {
//					Log.d("segment", "ok");
					Intent intent = new Intent(context,
							PhotoActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putExtra("data", galleryContents);
					intent.putExtra("pos", pos);
					context.startActivity(intent);
				} else {
					Log.d("segment", "ok");
					Intent intent = new Intent(context,
							VideoActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.putExtra("data",galleryContents.get(pos));
					context.startActivity(intent);
				}
			}
		});
 
		return listView;
	}
	
	public GalleryContent getGalleryContent(int position){
		return galleryContents.get(position);
	}
	
	private class ViewHolder{
		ImageView imgDesc, imgAvatar;
		TextView tvTitle, tvUserName, tvCreatedTime, tvLikes, tvComments, tvViewCount;
	}
	
}
